#!/user/bin/python
# _*_ coding: UTF-8 _*_
# Filename: testTaobao.py
import random
import re
import time
import pymongo
from selenium import webdriver


client = pymongo.MongoClient('mongodb://localhost:27017')
mydb = client["jdmpresystem"]
product = mydb['Suningproducts']


driver = webdriver.Chrome()
# 修改浏览器部分属性，绕过检测。
driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
    "source": """Object.defineProperty(navigator,'webdriver',{get:()=> false})"""
})
driver.get('https://www.suning.com/')
driver.implicitly_wait(10)  # 设置浏览器的等待（耗时） 智能化等待 加载完就不会等待
driver.maximize_window()
driver.find_element_by_xpath('//*[@id="searchKeywords"]').send_keys('手机')
driver.find_element_by_xpath('//*[@id="searchSubmit"]').click()


def drop_down():
    for x in range(1, 10, 2):
        j = x / 9
        js = 'document.documentElement.scrollTop = document.documentElement.scrollHeight * %f' % j
        driver.execute_script(js)
        time.sleep(0.5)


for i in range(0, 50):
    drop_down()
    time.sleep(2)
    # driver.find_element_by_css_selector('#bottomPage').send_keys('30')
    # ye = driver.find_element_by_css_selector('#bottom_pager > div > a.page-more.ensure')
    # driver.execute_script("arguments[0].click();", ye)
    # time.sleep(2)
    # drop_down()
    # time.sleep(2)
    lis = driver.find_elements_by_css_selector('#product-list > ul > li')
    for li in lis:
        shopname = li.find_element_by_css_selector('div > div > div.res-info > div.store-stock > a').text
        if shopname == '苏宁自营' or shopname == '苏宁服务':
            skuname = li.find_element_by_css_selector(' div > div > div.res-info > div.title-selling-point').text
            durl = li.find_element_by_css_selector(' div > div > div.res-img > div.img-block > a').get_attribute('href')
            price = li.find_element_by_css_selector(' div > div > div.res-info > div.price-box > span').text
            if price == '待发布' or price == '' or price =='敬请期待':
                price = -1
            else:
                price = price.split(".")[0].split('¥')[1]
                price = int(price)
            isco = li.find_element_by_css_selector('div > div > div.res-info > div.evaluate-old.clearfix').text
            if isco != '':
                comment = li.find_element_by_css_selector(
                    'div > div > div.res-info > div.evaluate-old.clearfix > div > a > i').text
            else:
                comment = 0
            imgurl = li.find_element_by_css_selector(' div > div > div.res-img > div.img-block > a > img').get_attribute('src')
            phone = {
                'price': price,
                'comment': comment,
                'skuname': skuname,
                'url': durl,
                'img':imgurl
            }
            x = product.insert_one(phone)
            print(x)
        else:
            continue
    if i == 49:
        break
    else:
        page = driver.find_element_by_css_selector('#nextPage')
        driver.execute_script("arguments[0].click();", page)
drop_down()
